package com.huajia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huajia.common.Result;
import com.huajia.entity.User;
import com.huajia.mapper.UserMapper;
import com.huajia.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public Result<?> getUserList(User user) {
        try {
            LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(User::getUserName, user.getUserName());
            queryWrapper.eq(User::getState,1);
            IPage<User> page = new Page<User>(1, 10);
            // 分页查询
            IPage<User> userIPage = this.baseMapper.selectPage(page, queryWrapper);
            return Result.data(userIPage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("查询失败");
    }

    @Override
    public Result<?> addUser(User user) {
        int result = userMapper.insert(user);
        if (result == 1) {
            return Result.success("新增用户成功");
        }
        return Result.fail("新增用户失败");

    }
}
